﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class BiaoShu_GL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";         
            
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {            
            this.TextBox_Pager.Text = (this.GridView1.PageIndex +1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }
           

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);           

        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
               
            }
        }


        protected void Button_New_Click(object sender, EventArgs e)
        {

            this.Panel4.Visible = true;
            this.Panel1.Visible = false;
            this.Panel2.Visible = false;
           
        }


        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("JiHua_MX.aspx?id=" + ((Button)sender).CommandName);
        //}

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {                
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                string ID =GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
                Cmd.CommandText = "delete 原料标书名 where ID=" + ID;
                Cmd.ExecuteNonQuery();
                Cmd.Dispose();
                Conn.Close();
                Conn.Dispose();
                GridView1.DeleteRow(GridView1.SelectedIndex);
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }
        ////明细
        //protected void Button_MX_Click(object sender, EventArgs e)
        //{
        //    //string 参数=((Button)sender).CommandName;
        //    //string[] Args = 参数.Split("\t".ToCharArray());
        //    //Response.Redirect("XunJiaGuanLi.aspx?BiaoShuID=" + Args[0] + "&&BiaoShuMing=" + Args[1] + "&&Visible=false");
          
        //}

        protected void Button_Query_Click(object sender, EventArgs e)
        {            
            string Sql = "";
            Sql = "[标书名称] like '%" + BiaoShuMing.Text+ "%'";
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                Sql += " and [原料标书名].[日期]>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                Sql += " and [原料标书名].[日期]<='" + Dt.ToShortDateString() + "'";
            }
            //if (this.GongHuoDanWei.Text != "0")
            //{
            //    Sql += " and [询价单].[供货单位ID]=" + this.GongHuoDanWei.Text;
            //}
            //if (this.YuanCaiLiao.Text != "0")
            //{
            //    Sql += " and [询价单].[货物ID]=" + this.YuanCaiLiao.Text;
            //}
            this.SqlDataSource3.SelectCommand = @"SELECT [ID]
      ,[标书名称]
      ,[日期]
  FROM [dbo].[原料标书名] where " + Sql;

            this.ViewState["SelectCommand"] = this.SqlDataSource3.SelectCommand;
        }

        ////protected void Unnamed1_Click(object sender, EventArgs e)
        ////{
        ////    this.Panel2.Visible = true;
        ////    this.Panel3.Visible = true;
        ////}

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string mc = this.MingCheng.Text.Replace("'", "").Trim();
            if (mc == "")
            {
                this.Message.Text = "·请输入计划名称";
                return;
            }
            DateTime d1;
            if (!DateTime.TryParse(DateTime.Today.ToString(), out d1))
            {
                this.Message.Text = "·请正确输入日期";
                return;
            }
            //else if (d1 > d2)
            //{
            //    this.Message.Text = "·截止日期需在起始日期之后";
            //}
            string ID = this.BiaoShuID.Text;
            if (ID == "")
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                Cmd.CommandText = "BiaoShu_New";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add(new SqlParameter("@标书名称", SqlDbType.NVarChar, 40, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, mc));
                Cmd.Parameters.Add(new SqlParameter("@投标日期", SqlDbType.DateTime, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, d1));
                Cmd.Parameters.Add(new SqlParameter("@标书类别", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                if (Cmd.ExecuteNonQuery() > 0)
                {

                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.Message.Text = "中标书创建成功";
                    Response.Redirect("BiaoShu_GL.aspx");
                    //Button b = (Button)sender;
                    //Response.Redirect("XunJiaGuanLi.aspx?BiaoShuID=" + ((int)Cmd.Parameters["@ID"].Value).ToString() + "&BiaoShuMing=" + mc);
                }
                else
                {
                    this.Message.Text = "中标书创建失败!";
                    return;
                }
                Cmd.Dispose();
                Conn.Close();
                Conn.Dispose();
            }
            else
            {  
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                string sql = "update 原料标书名 set 标书名称=@标书名称 where ID=@ID";
                sql = sql.Replace("@标书名称", "'"+this.MingCheng.Text+"'");
                sql = sql.Replace("@ID", ID);
                Cmd.CommandText = sql;
                if (Cmd.ExecuteNonQuery() > 0)
                {

                    this.Panel1.Visible = true;
                    this.Panel2.Visible = false;
                    this.Message.Text = "中标书修改成功";
                    Response.Redirect("BiaoShu_GL.aspx");
                    //Button b = (Button)sender;
                    //Response.Redirect("XunJiaGuanLi.aspx?BiaoShuID=" + ((int)Cmd.Parameters["@ID"].Value).ToString() + "&BiaoShuMing=" + mc);
                }
                else
                {
                    this.Message.Text = "中标书修改失败!";
                    return;
                }
                 Cmd.Dispose();
                Conn.Close();
                Conn.Dispose();
            }
        }     

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("BiaoShu_GL.aspx");
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {                          
                
                string ID = "";
                ID = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();               
                //Button bt = (Button)this.GridView1.Rows[GridView1.SelectedIndex].FindControl("Button");
                //string 参数 = bt.CommandName;
                //string[] Args = 参数.Split("\t".ToCharArray());
                //Response.Redirect("XunJiaGuanLi.aspx?BiaoShuID=" + ID + "&&BiaoShuMing=" + Args[1] + "&&Visible=false");                
                Response.Redirect("原料标书录入.aspx?BiaoShuID=" + ID + "&&BiaoShuMing=" + this.GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text);
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        protected void Button_Modify_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                string ID = "";
                ID = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();          
                this.Panel4.Visible = true;
                this.Panel4.GroupingText = "标书名称 修改";
                this.Panel1.Visible = false;
                this.Panel2.Visible = false;
                this.MingCheng.Text = this.GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text;
                this.BiaoShuID.Text = ID;
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
       
        }
       
    }
}
